package com.oa.modules.approvalsec1.socket;

import com.google.gson.Gson;
import io.socket.client.IO;
import io.socket.client.Socket;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Hankaicheng
 * @Date: 2022/3/2 10:50
 */
@Component
@Log4j2
public class SocketServer {
    public static void main(String[] args) throws URISyntaxException {
        String url = "http://120.224.193.89:9083";
        Map body = new HashMap();
        body.put("type", "text");
        body.put("content", "a");
        body.put("senderId", "8a7c2f1b-c21e-43a9-a662-8bbf44e8ac8e");
        body.put("senderName", "visitor");
        Gson gson = new Gson();
        String str = gson.toJson(body);
        IO.Options options = new IO.Options();
        options.transports = new String[]{"websocket"};
//        options.reconnectionAttempts = 2;
//        options.reconnectionDelay = 1000;
//        options.timeout = 500;
//        Socket socket = IO.socket(url + "/websocket?userType=visitor&userId=8a7c2f1b-c21e-43a9-a662-8bbf44e8ac8e", options);
       final Socket socket = IO.socket("http://120.224.193.89:9083/websocket?userType=visitor&userId=860e1f84-9231-4e02-ab3a-c5575704b815", options);
        socket.open();
        socket.on(Socket.EVENT_CONNECT, (args1) -> socket.send(str));
        socket.on("push_data_event", objects -> System.out.println(objects[0].toString()));
        System.out.println(socket.connected());

    }

}
